import http from '../request'
import type {
    SpaceCreateDTO,
    SpaceJoinDTO,
    SpaceTransferDTO,
    SpaceUpdateDTO,
    SpaceUpgradeDTO,
    SpaceUserRoleUpdateDTO
} from '@/types/dto/space'
import type {MySpaceVO, SpaceInviteVO, SpaceUserListVO, SpaceVO} from '@/types/vo/space'

export const spaceApi = {
    /** 创建空间 */
    create(payload: SpaceCreateDTO) {
        return http.post<SpaceVO>('/space', payload)
    },

    /** 我的空间列表 */
    my() {
        return http.get<MySpaceVO>('/space/my')
    },

    /** 我的空间列表（别名，为了兼容） */
    getMySpaces() {
        return this.my()
    },

    /** 空间详情 */
    getById(id: number) {
        return http.get<SpaceVO>(`/space/${id}`)
    },

    /** 更新空间信息 */
    update(id: number, payload: SpaceUpdateDTO) {
        return http.put<SpaceVO>(`/space/${id}`, payload)
    },

    /** 升级空间级别 */
    upgrade(id: number, payload: SpaceUpgradeDTO) {
        return http.post<SpaceVO>(`/space/${id}/upgrade`, payload)
    },

    /** 生成邀请码 */
    generateInviteCode(id: number) {
        return http.post<SpaceInviteVO>(`/space/${id}/invite`)
    },

    /** 加入空间 */
    join(payload: SpaceJoinDTO) {
        return http.post<SpaceVO>('/space/join', payload)
    },

    /** 空间成员列表 */
    getMembers(id: number) {
        return http.get<SpaceUserListVO>(`/space/${id}/member`)
    },

    /** 修改成员角色 */
    updateMemberRole(spaceId: number, userId: number, payload: SpaceUserRoleUpdateDTO) {
        return http.patch<void>(`/space/${spaceId}/member/${userId}/role`, payload)
    },

    /** 踢出成员 */
    removeMember(spaceId: number, userId: number) {
        return http.delete<void>(`/space/${spaceId}/member/${userId}`)
    },

    /** 转让空间 */
    transfer(id: number, payload: SpaceTransferDTO) {
        return http.put<void>(`/space/${id}/transfer`, payload)
    },

    /** 删除空间 */
    delete(id: number) {
        return http.delete<void>(`/space/${id}`)
    },
}
